package fitness;

import java.io.Serializable;

import util.MyUtil;
import model.Constants;
import model.OligoSystemGeneral;

public class ChaoticFitness extends Fitness implements Serializable {

	@Override
	public FitnessResult evaluateFitness(OligoSystemGeneral oligoModel) {
		ChaoticResult result = new ChaoticResult();
		try {
			int output = oligoModel.nameToInt.get("a");
			double[][] data = oligoModel.getTimeSeriesComplexModelTillStable();
			double max = 0;
			for (int i = 0; i < data[output].length; i++) {
				if (max < data[output][i]) {
					max = data[output][i];
				}
			}
			double[] data1 = new double[1000];
			double[] data2 = new double[2000];
			for (int i = 0; i < data1.length; i++) {
				data1[i] = data[output][2800 + i] / max;
			}
			for (int i = 0; i < data2.length; i++) {
				data2[i] = data[output][2800 + i] / max;
			}
			double[] entropy1 = MyUtil.sampleEntropy(data1, 10, 0.1);
			double[] entropy2 = MyUtil.sampleEntropy(data2, 10, 0.1);
			double sum1 = 0;
			double sum2 = 0;
			for (int i = 0; i < entropy1.length; i++) {
				sum1 += entropy1[i];
				sum2 += entropy2[i];
			}
			if (sum1 > sum2 * 1.05 || sum1 < sum2 * 0.95) {
				result = minFitness(oligoModel);
			} else {
				result.entropy = entropy2;
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return result;
	}

	@Override
	public FitnessResult evaluateFitnessSimple(OligoSystemGeneral indiv) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public ChaoticResult minFitness(OligoSystemGeneral indiv) {
		ChaoticResult result = new ChaoticResult();
		result.entropy = new double[] { 0 };
		return result;
	}
}
